package com.plian.Tools.office;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
public  class PDFUtil {

    /**
     *
     * @param res 响应对象
     * @param name 文件名称
     * @throws Exception
     */
    public PDFUtil(HttpServletResponse res,String name, String BASE_PATH) throws Exception{
        // 设置响应头和客户端保存文件名
        res.setCharacterEncoding("utf-8");
        res.setContentType("application/octet-stream");
        String n = name.substring(19);
        String na = new String(n.getBytes("utf-8"), "iso-8859-1");
        res.setHeader("Content-Disposition", "attachment;fileName=" + na);
        String separator= File.separator;
        // 打开本地文件流
        InputStream inputStream = new FileInputStream(BASE_PATH+separator+name);
        // 激活下载操作
        OutputStream os = res.getOutputStream();
        // 循环写入输出流
        byte[] b = new byte[2048];
        int length;
        while ((length = inputStream.read(b)) > 0) {
            os.write(b, 0, length);
        }
        // 这里主要关闭。
        os.close();
        inputStream.close();
    }



}
